-- ============================================================
-- 参数
-- ============================================================
local room_id = KEYS[1]
local user_id = KEYS[2]
-- ============================================================
-- 房间信息
-- ============================================================
if redis.call('exists', 'wooha-cache-hub:room:info:' .. room_id) == 0 then
    return {1, "房间不存在"}
end
local room_info = cjson.decode(redis.call('get', 'wooha-cache-hub:room:info:' .. room_id))
-- ============================================================
-- 团购队列
-- ============================================================
if redis.call('exists', 'wooha-cache-hub:room:queue:' .. room_id) == 0 then
    return {1, "团购队列不存在"}
end
local queue_list = cjson.decode(redis.call('get', 'wooha-cache-hub:room:queue:' .. room_id))
-- ============================================================
-- 房间当前团购
-- ============================================================
local current_group_buying
for index, queue in ipairs(queue_list) do
    if queue['id'] == room_info['queueId'] then
        current_group_buying = queue
        break
    end
end
if current_group_buying == nil then
    return {1, "房间当前团购不存在"}
end
-- ============================================================
-- 房间下一轮团购
-- ============================================================
local next_group_buying
for index, queue in ipairs(queue_list) do
    if tonumber(queue['id']) > tonumber(current_group_buying['id']) then
        next_group_buying = queue
        break
    end
end
if next_group_buying == nil then
    return {1, "下一轮团购不存在"}
end
-- ============================================================
-- 房间号
-- ============================================================
local num = redis.call('hlen', 'shop:Room:' .. room_id)
-- ============================================================
-- 最大票数, 当前票数, 用户已投票数, 用户最大票数
-- ============================================================
local user_max_tickets = redis.call('get', 'GroupBuying:UserMaxTickets:_' .. current_group_buying['groupBuyingId'])
local current_tickets = redis.call('get', 'GroupBuying:CurrentTickets:_' .. current_group_buying['groupBuyingId'])
local max_tickets = redis.call('get', 'GroupBuying:MaxTickets:_' .. current_group_buying['groupBuyingId'])
local user_current_tickets = 0
if redis.call('exists', 'get',
    'GroupBuying:UserCurrentTickets:_' .. current_group_buying['groupBuyingId'] .. '_' .. user_id) == 1 then
    user_current_tickets = redis.call('get',
        'GroupBuying:UserCurrentTickets:_' .. current_group_buying['groupBuyingId'] .. '_' .. user_id)
end
-- ============================================================
-- 数据组装
-- ============================================================
local result = {}
result['id'] = room_info['id']
result['name'] = room_info['name']
result['i18n'] = room_info['i18n']
result['queueId'] = room_info['queueId']
result['defaultProduct'] = room_info['defaultProduct']
result['isStop'] = room_info['isStop']
result['isDeleted'] = room_info['isDeleted']
result['createTime'] = room_info['createTime']
result['updateTime'] = room_info['updateTime']
result['prevGroupBuyingId'] = tonumber(room_info['prevGroupBuyingId'])
result['num'] = num
result['brandName'] = current_group_buying['brandName']
result['productName'] = current_group_buying['productName']
result['specificationValues'] = current_group_buying['specificationValues']
result['productId'] = tonumber(current_group_buying['productId'])
result['img'] = current_group_buying['img']
result['favorite'] = false
result['groupBuyingId'] = tonumber(current_group_buying['groupBuyingId'])
result['userMaxTickets'] = tonumber(user_max_tickets)
result['userTotalTickets'] = tonumber(user_current_tickets)
result['currentTickets'] = tonumber(current_tickets)
result['progress'] = current_group_buying['progress']
result['maxTickets'] = tonumber(max_tickets)
result['groupBuyingStatus'] = current_group_buying['groupBuyingStatus']
result['beginDate'] = current_group_buying['beginDate']
result['nextProductName'] = next_group_buying['productName']
result['nextProductImg'] = next_group_buying['img']
-- 用户收藏
local user_favorite = {}
if redis.call('exists', 'wooha-cache-hub:user:favorite:' .. user_id) == 1 then
    user_favorite = cjson.decode(redis.call('get', 'wooha-cache-hub:user:favorite:' .. user_id))
end
for index, product_id in ipairs(user_favorite) do
    if product_id == current_group_buying['productId'] then
        result['favorite'] = true
        break
    end
end
return {0, cjson.encode(result)}
